package com.opengms.HydroModelsIntegration.Dao;
import com.opengms.HydroModelsIntegration.Domain.InpData;

import java.io.*;

public class InpWriter {
    //组合类，或者也可以让InpDataWriteUtil继承Inpdata
    public InpData inpData=new InpData();

    private final static String EOL=System.getProperty("line.separator");

    public final void writeInp(String fileName)
    {
        try {
            File fileOut=new File(fileName);
//          FileWriter writer = new FileWriter("F:\\Test\\writeInp.txt");

            OutputStreamWriter outputStreamWriter=null;
            outputStreamWriter=new OutputStreamWriter(new FileOutputStream(fileOut),"UTF-8");
            BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);

            writeTitle(bufferedWriter);
            writeOptions(bufferedWriter);
            writeEvaporation(bufferedWriter);
            writeRaingage(bufferedWriter);
            writeSubcatchment(bufferedWriter);
            writeSubarea(bufferedWriter);
            writeInfiltration(bufferedWriter);
            writeJunction(bufferedWriter);
            writeOutfall(bufferedWriter);
            writeStorage(bufferedWriter);
            writeConduit(bufferedWriter);
            writeXscetion(bufferedWriter);
            writeLosses(bufferedWriter);
            writeCurve(bufferedWriter);
            writeTimeseries(bufferedWriter);
            writeReport(bufferedWriter);
            writeTag(bufferedWriter);
            writeMap(bufferedWriter);
            writeCoordinate(bufferedWriter);
            writeVertice(bufferedWriter);
            writePolygon(bufferedWriter);
            writeSymbol(bufferedWriter);

            bufferedWriter.close();
//          writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void writeTitle(BufferedWriter bufferedWriter)
    {
        try {
            bufferedWriter.write("[TITLE]"+EOL);
            bufferedWriter.write(";;Project Title/Notes"+EOL);
            for (int i=0;i<inpData.getTitleArrayList().size();i++)
            {
                bufferedWriter.write(inpData.getTitleArrayList().get(i).toString()+EOL);
            }
            bufferedWriter.newLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void writeOptions(BufferedWriter bufferedWriter)
    {
        try {
            bufferedWriter.write("[OPTIONS]"+EOL);
            bufferedWriter.write(String.format("%-21s %-21s"+EOL,";;Option","Value"));
            bufferedWriter.write(String.format("%-21s %-21s"+EOL,"FLOW_UNITS",inpData.getOption().getFlow_units()));
            bufferedWriter.write(String.format("%-21s %-21s"+EOL,"INFILTRATION",inpData.getOption().getInfiltration()));
            bufferedWriter.write(String.format("%-21s %-21s"+EOL,"FLOW_ROUTING",inpData.getOption().getFlow_routing()));
            bufferedWriter.write(String.format("%-21s %-21s"+EOL,"LINK_OFFSETS",inpData.getOption().getLink_offsets()));
            bufferedWriter.write(String.format("%-21s %-21s"+EOL,"MIN_SLOPE",inpData.getOption().getMin_slope()));
            bufferedWriter.write(String.format("%-21s %-21s"+EOL,"ALLOW_PONDING",inpData.getOption().getAllow_ponding()));
            bufferedWriter.write(String.format("%-21s %-21s"+EOL,"SKIP_STEADY_STATE",inpData.getOption().getSkip_steady_state()));
            bufferedWriter.newLine();

            if(inpData.getOption().getIgnore_rainfall().equals("YES"))
            {
                bufferedWriter.write(String.format("%-21s %-21s"+EOL,"IGNORE_RAINFALL",inpData.getOption().getIgnore_rainfall()));
            }
            if(inpData.getOption().getIgnore_routing().equals("YES"))
            {
                bufferedWriter.write(String.format("%-21s %-21s"+EOL,"IGNORE_ROUTING",inpData.getOption().getIgnore_routing()));
            }
            bufferedWriter.write(String.format("%-21s %-21s"+EOL,"START_DATE",inpData.getOption().getStart_date()));
            bufferedWriter.write(String.format("%-21s %-21s"+EOL,"START_TIME",inpData.getOption().getStart_time()));
            bufferedWriter.write(String.format("%-21s %-21s"+EOL,"REPORT_START_DATE",inpData.getOption().getReport_start_date()));
            bufferedWriter.write(String.format("%-21s %-21s"+EOL,"REPORT_START_TIME",inpData.getOption().getReport_start_time()));
            bufferedWriter.write(String.format("%-21s %-21s"+EOL,"END_DATE",inpData.getOption().getEnd_date()));
            bufferedWriter.write(String.format("%-21s %-21s"+EOL,"END_TIME",inpData.getOption().getEnd_time()));
            bufferedWriter.write(String.format("%-21s %-21s"+EOL,"SWEEP_START",inpData.getOption().getSweep_start()));
            bufferedWriter.write(String.format("%-21s %-21s"+EOL,"SWEEP_END",inpData.getOption().getSweep_end()));
            bufferedWriter.write(String.format("%-21s %-21s"+EOL,"DRY_DAYS",inpData.getOption().getDry_days()));
            bufferedWriter.write(String.format("%-21s %-21s"+EOL,"REPORT_STEP",inpData.getOption().getReport_step()));
            bufferedWriter.write(String.format("%-21s %-21s"+EOL,"WET_STEP",inpData.getOption().getWet_step()));
            bufferedWriter.write(String.format("%-21s %-21s"+EOL,"DRY_STEP",inpData.getOption().getDry_step()));
            bufferedWriter.write(String.format("%-21s %-21s"+EOL,"ROUTING_STEP",inpData.getOption().getRouting_step()));
            bufferedWriter.newLine();

            bufferedWriter.write(String.format("%-21s %-21s"+EOL,"INERTIAL_DAMPING",inpData.getOption().getInertial_damping()));
            bufferedWriter.write(String.format("%-21s %-21s"+EOL,"NORMAL_FLOW_LIMITED",inpData.getOption().getNormal_flow_limited()));
            bufferedWriter.write(String.format("%-21s %-21s"+EOL,"FORCE_MAIN_EQUATION",inpData.getOption().getForce_main_equation()));
            bufferedWriter.write(String.format("%-21s %-21s"+EOL,"VARIABLE_STEP",inpData.getOption().getVariable_step()));
            bufferedWriter.write(String.format("%-21s %-21s"+EOL,"LENGTHENING_STEP",inpData.getOption().getLengthening_step()));
            bufferedWriter.write(String.format("%-21s %-21s"+EOL,"MIN_SURFAREA",inpData.getOption().getMin_surfarea()));
            bufferedWriter.write(String.format("%-21s %-21s"+EOL,"MAX_TRIALS",inpData.getOption().getMax_trials()));
            bufferedWriter.write(String.format("%-21s %-21s"+EOL,"HEAD_TOLERANCE",inpData.getOption().getHead_tolerance()));
            bufferedWriter.write(String.format("%-21s %-21s"+EOL,"SYS_FLOW_TOL",inpData.getOption().getSys_flow_tol()));
            bufferedWriter.write(String.format("%-21s %-21s"+EOL,"LAT_FLOW_TOL",inpData.getOption().getLat_flow_tol()));
            bufferedWriter.newLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //TODO 目前只支持到evaporation的constant，dryonly，暂不支持其他变化
    private void writeEvaporation(BufferedWriter bufferedWriter)
    {
        try {
            bufferedWriter.write("[EVAPORATION]"+EOL);
            bufferedWriter.write(String.format("%-18s %-21s"+EOL,";;Evap Data","Parameters"));
            bufferedWriter.write(String.format("%-18s %-21s"+EOL,";;--------------","----------------"));
            bufferedWriter.write(String.format("%-18s %-21s"+EOL,"CONSTANT",inpData.getEvaporation().getConstant()));
            bufferedWriter.write(String.format("%-18s %-21s"+EOL,"DRY_ONLY",inpData.getEvaporation().getDryonly()));
            bufferedWriter.newLine();
//            bufferedWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void writeRaingage(BufferedWriter bufferedWriter)
    {
        try {
            bufferedWriter.write("[RAINGAGES]"+EOL);
            bufferedWriter.write(String.format("%-18s %-18s %-18s %-18s %-18s"+EOL,";;Gage","Format","Interval","SCF","Source"));
            bufferedWriter.write(String.format("%-18s %-18s %-18s %-18s %-18s"+EOL,";;--------------","--------------","--------------","--------------","--------------"));
            for(int i=0,len=inpData.getRaingageArrayList().size();i<len;i++)
            {
                if(inpData.getRaingageArrayList().get(i).getDescription().size()!=0)
                {
                    for(int j=0;j<inpData.getRaingageArrayList().get(i).getDescription().size();j++)
                    {
                        bufferedWriter.write(String.format("%s%s"+EOL,";",inpData.getRaingageArrayList().get(i).getDescription().get(j)));
                    }
                }
                bufferedWriter.write(String.format("%-18s %-18s %-18s %-18s",inpData.getRaingageArrayList().get(i).getGage(),inpData.getRaingageArrayList().get(i).getFormat(),inpData.getRaingageArrayList().get(i).getInterval(),inpData.getRaingageArrayList().get(i).getScf()));
                if(inpData.getRaingageArrayList().get(i).getDatasource().equals("TIMESERIES"))
                {
                    bufferedWriter.write(String.format(" %-18s %s"+EOL,inpData.getRaingageArrayList().get(i).getDatasource(),inpData.getRaingageArrayList().get(i).getSeriesname()));
                }
                else if(inpData.getRaingageArrayList().get(i).getDatasource().equals("FILE"))
                {
                    bufferedWriter.write(String.format(" %-18s %s %s     %s"+EOL,inpData.getRaingageArrayList().get(i).getDatasource(),inpData.getRaingageArrayList().get(i).getFilename(),inpData.getRaingageArrayList().get(i).getStationid(),inpData.getRaingageArrayList().get(i).getRainunits()));
                }
            }
            bufferedWriter.newLine();
            bufferedWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void writeSubcatchment(BufferedWriter bufferedWriter)
    {
        try {
            bufferedWriter.write("[SUBCATCHMENTS]"+EOL);
            bufferedWriter.write(String.format("%-18s %-18s %-18s %-14s %-14s %-14s %-14s %-14s %-14s"+EOL,";;Subcatchment","Rain Gage","Outlet","Area","%Imperv","Width","%Slope","CurbLen","Snow Pack"));
            bufferedWriter.write(String.format("%-18s %-18s %-18s %-14s %-14s %-14s %-14s %-14s %-14s"+EOL,";;--------------","----------------","----------------","-------------","-------------","-------------","-------------","-------------","-------------"));
            for(int i=0;i<inpData.getSubcatchmentArrayList().size();i++)
            {
                if(inpData.getSubcatchmentArrayList().get(i).getDescription().size()!=0)
                {

                    for(int j=0;j<inpData.getSubcatchmentArrayList().get(i).getDescription().size();j++)
                    {
                        bufferedWriter.write(String.format("%s%s"+EOL,";",inpData.getSubcatchmentArrayList().get(i).getDescription().get(j)));
                    }
                }

                bufferedWriter.write(String.format(String.format("%-18s %-18s %-18s %-14s %-14s %-14s %-14s %-14s %-14s"+EOL,inpData.getSubcatchmentArrayList().get(i).getSubcatchment(),
                        inpData.getSubcatchmentArrayList().get(i).getRaingage(),inpData.getSubcatchmentArrayList().get(i).getOutlet(),inpData.getSubcatchmentArrayList().get(i).getArea(),
                        inpData.getSubcatchmentArrayList().get(i).getImperv(),inpData.getSubcatchmentArrayList().get(i).getWidth(),inpData.getSubcatchmentArrayList().get(i).getSlope(),
                        inpData.getSubcatchmentArrayList().get(i).getCurblen(),inpData.getSubcatchmentArrayList().get(i).getSnowpack())));

            }
            bufferedWriter.newLine();
            bufferedWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void writeSubarea(BufferedWriter bufferedWriter)
    {
        try {
            bufferedWriter.write("[SUBAREAS]"+EOL);
            bufferedWriter.write(String.format("%-18s %-18s %-18s %-14s %-14s %-14s %-14s %-14s"+EOL,";;Subcatchment","N-Imperv","N-Perv","S-Imperv","S-Perv","PctZero","RouteTo","PctRouted"));
            bufferedWriter.write(String.format("%-18s %-18s %-18s %-14s %-14s %-14s %-14s %-14s"+EOL,";;--------------","----------------","----------------","-------------","-------------","-------------","-------------","-------------"));
            for(int i=0;i<inpData.getSubareaArrayList().size();i++)
            {
                if(inpData.getSubareaArrayList().get(i).getPctrouted()-100<0.001) {
                    bufferedWriter.write(String.format(String.format("%-18s %-18s %-18s %-14s %-14s %-14s %-14s" + EOL, inpData.getSubareaArrayList().get(i).getSubcatchment(),
                            inpData.getSubareaArrayList().get(i).getN_imperv(), inpData.getSubareaArrayList().get(i).getN_perv(), inpData.getSubareaArrayList().get(i).getS_imperv(),
                            inpData.getSubareaArrayList().get(i).getS_perv(), inpData.getSubareaArrayList().get(i).getPctzero(), inpData.getSubareaArrayList().get(i).getRouteto())));
                }
                else
                {
                    bufferedWriter.write(String.format(String.format("%-18s %-18s %-18s %-14s %-14s %-14s %-14s %-14s" + EOL, inpData.getSubareaArrayList().get(i).getSubcatchment(),
                            inpData.getSubareaArrayList().get(i).getN_imperv(), inpData.getSubareaArrayList().get(i).getN_perv(), inpData.getSubareaArrayList().get(i).getS_imperv(),
                            inpData.getSubareaArrayList().get(i).getS_perv(), inpData.getSubareaArrayList().get(i).getPctzero(), inpData.getSubareaArrayList().get(i).getRouteto(),
                            inpData.getSubareaArrayList().get(i).getPctrouted())));
                }
            }
            bufferedWriter.newLine();
            bufferedWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void writeInfiltration(BufferedWriter bufferedWriter)
    {
        try {
            bufferedWriter.write("[INFILTRATION]"+EOL);
            bufferedWriter.write(String.format("%-18s %-18s %-18s %-14s %-14s %-14s"+EOL,";;Subcatchment","MaxRate","MinRate","Decay","DryTime","MaxInfil"));
            bufferedWriter.write(String.format("%-18s %-18s %-18s %-14s %-14s %-14s"+EOL,";;--------------","----------------","----------------","-------------","-------------","-------------"));
            for(int i=0;i<inpData.getInfiltrationArrayList().size();i++)
            {
                bufferedWriter.write(String.format(String.format("%-18s %-18s %-18s %-14s %-14s %-14s" + EOL, inpData.getInfiltrationArrayList().get(i).getSubcatchment(),
                        inpData.getInfiltrationArrayList().get(i).getMaxrate(), inpData.getInfiltrationArrayList().get(i).getMinrate(), inpData.getInfiltrationArrayList().get(i).getDecay(),
                        inpData.getInfiltrationArrayList().get(i).getDrytime(), inpData.getInfiltrationArrayList().get(i).getMaxinfil())));

            }
            bufferedWriter.newLine();
            bufferedWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void writeJunction(BufferedWriter bufferedWriter)
    {
        try {
            bufferedWriter.write("[JUNCTIONS]"+EOL);
            bufferedWriter.write(String.format("%-18s %-18s %-18s %-14s %-14s %-14s"+EOL,";;Junction","Invert","Outlet","MaxDepth","InitDepth","SurDepth","Aponded"));
            bufferedWriter.write(String.format("%-18s %-18s %-18s %-14s %-14s %-14s"+EOL,";;--------------","----------------","----------------","-------------","-------------","-------------"));
            for(int i=0;i<inpData.getJunctionArrayList().size();i++)
            {
                if(inpData.getJunctionArrayList().get(i).getDescription().size()!=0)
                {
                    for(int j=0;j<inpData.getJunctionArrayList().get(i).getDescription().size();j++)
                    {
                        bufferedWriter.write(String.format("%s%s"+EOL,";",inpData.getJunctionArrayList().get(i).getDescription().get(j)));
                    }
                }
                bufferedWriter.write(String.format(String.format("%-18s %-18s %-18s %-14s %-14s %-14s"+EOL,inpData.getJunctionArrayList().get(i).getJunction(),
                        inpData.getJunctionArrayList().get(i).getInvert(),inpData.getJunctionArrayList().get(i).getMaxdepth(),inpData.getJunctionArrayList().get(i).getInitdepth(),
                        inpData.getJunctionArrayList().get(i).getSurdepth(),inpData.getJunctionArrayList().get(i).getAponded())));

            }
            bufferedWriter.newLine();
            bufferedWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void writeOutfall(BufferedWriter bufferedWriter)
    {
        try {
            bufferedWriter.write("[OUTFALLS]"+EOL);
            bufferedWriter.write(String.format("%-18s %-18s %-18s %-14s %-14s"+EOL,";;Outfall","Invert","Type","Stage Data","Gated"));
            bufferedWriter.write(String.format("%-18s %-18s %-18s %-14s %-14s"+EOL,";;--------------","----------------","----------------","-------------","-------------"));
            for(int i=0;i<inpData.getOutfallArrayList().size();i++)
            {
                if(inpData.getOutfallArrayList().get(i).getDescription().size()!=0)
                {
                    for(int j=0;j<inpData.getOutfallArrayList().get(i).getDescription().size();j++)
                    {
                        bufferedWriter.write(String.format("%s%s"+EOL,";",inpData.getOutfallArrayList().get(i).getDescription().get(j)));
                    }
                }
                bufferedWriter.write(String.format(String.format("%-18s %-18s %-18s %-14s %-14s"+EOL,inpData.getOutfallArrayList().get(i).getOutfall(),
                        inpData.getOutfallArrayList().get(i).getInvert(),inpData.getOutfallArrayList().get(i).getType(),inpData.getOutfallArrayList().get(i).getStagedata(),
                        inpData.getOutfallArrayList().get(i).getGated())));

            }
            bufferedWriter.newLine();
            bufferedWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void writeStorage(BufferedWriter bufferedWriter)
    {
        try {
            bufferedWriter.write("[STORAGE]"+EOL);
            bufferedWriter.write(String.format("%-18s %-18s %-18s %-14s %-14s %-29s %-14s %-14s %-14s"+EOL,";;Storage TreeNode","Invert","MaxDepth","InitDepth","Shape","Curve Name/Params","Ponded Area","Fevap","Seepage"));
            bufferedWriter.write(String.format("%-18s %-18s %-18s %-14s %-14s %-29s %-14s %-14s %-14s"+EOL,";;--------------","----------------","----------------","-------------","-------------","----------------------------","-------------","-------------","-------------"));
            for(int i=0;i<inpData.getStorageArrayList().size();i++)
            {
                if(inpData.getStorageArrayList().get(i).getDescription().size()!=0)
                {
                    for(int j=0;j<inpData.getStorageArrayList().get(i).getDescription().size();j++)
                    {
                        bufferedWriter.write(String.format("%s%s"+EOL,";",inpData.getStorageArrayList().get(i).getDescription().get(j)));
                    }
                }
                if(inpData.getStorageArrayList().get(i).getShape().equals("TABULAR"))
                {

                    bufferedWriter.write(String.format(String.format("%-18s %-18s %-18s %-14s %-14s %-29s %-14s %-14s",inpData.getStorageArrayList().get(i).getStorage(),
                            inpData.getStorageArrayList().get(i).getInvert(),inpData.getStorageArrayList().get(i).getMaxdepth(),inpData.getStorageArrayList().get(i).getInitdepth(),
                            inpData.getStorageArrayList().get(i).getShape(),inpData.getStorageArrayList().get(i).getCurvename(),inpData.getStorageArrayList().get(i).getPonded(),
                            inpData.getStorageArrayList().get(i).getFevap())));
                }
                else if(inpData.getStorageArrayList().get(i).getShape().equals("FUNCTIONAL"))
                {

                    bufferedWriter.write(String.format(String.format("%-18s %-18s %-18s %-14s %-14s %-9s %-9s %-9s %-14s %-14s",inpData.getStorageArrayList().get(i).getStorage(),
                            inpData.getStorageArrayList().get(i).getInvert(),inpData.getStorageArrayList().get(i).getMaxdepth(),inpData.getStorageArrayList().get(i).getInitdepth(),
                            inpData.getStorageArrayList().get(i).getShape(),inpData.getStorageArrayList().get(i).getParam()[0],inpData.getStorageArrayList().get(i).getParam()[1],
                            inpData.getStorageArrayList().get(i).getParam()[2],inpData.getStorageArrayList().get(i).getPonded(),inpData.getStorageArrayList().get(i).getFevap())));
                }
                else {

                }

                if (inpData.getStorageArrayList().get(i).getSeepage_exist().equals("YES"))
                {
                    bufferedWriter.write(String.format(String.format("%-9s %-9s %-9s",inpData.getStorageArrayList().get(i).getSeepage()[0],
                            inpData.getStorageArrayList().get(i).getSeepage()[1],inpData.getStorageArrayList().get(i).getSeepage()[2])));
                }

                bufferedWriter.write(EOL);

            }
            bufferedWriter.newLine();
            bufferedWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void writeConduit(BufferedWriter bufferedWriter)
    {
        try {
            bufferedWriter.write("[CONDUITS]"+EOL);
            bufferedWriter.write(String.format("%-18s %-18s %-18s %-14s %-14s %-14s %-14s %-14s %-14s"+EOL,";;Conduit","From TreeNode","To TreeNode","Length","Roughness","InOffset","OutOffset","InitFlow","MaxFlow"));
            bufferedWriter.write(String.format("%-18s %-18s %-18s %-14s %-14s %-14s %-14s %-14s %-14s"+EOL,";;--------------","----------------","----------------","-------------","-------------","-------------","-------------","-------------","-------------"));
            for(int i=0;i<inpData.getConduitArrayList().size();i++)
            {
                if(inpData.getConduitArrayList().get(i).getDescription().size()!=0)
                {
                    for(int j=0;j<inpData.getConduitArrayList().get(i).getDescription().size();j++)
                    {
                        bufferedWriter.write(String.format("%s%s"+EOL,";",inpData.getConduitArrayList().get(i).getDescription().get(j)));
                    }
                }
                bufferedWriter.write(String.format(String.format("%-18s %-18s %-18s %-14s %-14s %-14s %-14s %-14s %-14s"+EOL,inpData.getConduitArrayList().get(i).getConduit(),
                        inpData.getConduitArrayList().get(i).getFromnode(),inpData.getConduitArrayList().get(i).getTonode(),inpData.getConduitArrayList().get(i).getLength(),
                        inpData.getConduitArrayList().get(i).getRoughness(),inpData.getConduitArrayList().get(i).getInoffset(),inpData.getConduitArrayList().get(i).getOutoffset(),
                        inpData.getConduitArrayList().get(i).getInitflow(),inpData.getConduitArrayList().get(i).getMaxflow())));

            }
            bufferedWriter.newLine();
            bufferedWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void writeXscetion(BufferedWriter bufferedWriter)
    {
        try {
            bufferedWriter.write("[XSECTIONS]"+EOL);
            bufferedWriter.write(String.format("%-18s %-18s %-18s %-14s %-14s %-14s %-14s"+EOL,";;Link","Shape","Geom1","Geom2","Geom3","Geom4","Barrels"));
            bufferedWriter.write(String.format("%-18s %-18s %-18s %-14s %-14s %-14s %-14s"+EOL,";;--------------","----------------","----------------","-------------","-------------","-------------","-------------"));
            for(int i=0;i<inpData.getXscetionArrayList().size();i++)
            {

                bufferedWriter.write(String.format(String.format("%-18s %-18s %-18s %-14s %-14s %-14s %-14s %-14s"+EOL,inpData.getXscetionArrayList().get(i).getLink(),
                        inpData.getXscetionArrayList().get(i).getShape(),inpData.getXscetionArrayList().get(i).getGeom1(),inpData.getXscetionArrayList().get(i).getGeom2(),
                        inpData.getXscetionArrayList().get(i).getGeom3(),inpData.getXscetionArrayList().get(i).getGeom4(),inpData.getXscetionArrayList().get(i).getBarrels(),
                        inpData.getXscetionArrayList().get(i).getCulvertcode())));

            }
            bufferedWriter.newLine();
            bufferedWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void writeLosses(BufferedWriter bufferedWriter)
    {
        try {
            bufferedWriter.write("[LOSSES]"+EOL);
            bufferedWriter.write(String.format("%-18s %-18s %-18s %-14s %-14s %-14s"+EOL,";;Link","Kinlet","Kinlet","Kavg","Flap Gate","SeepRate"));
            bufferedWriter.write(String.format("%-18s %-18s %-18s %-14s %-14s %-14s"+EOL,";;--------------","----------------","----------------","-------------","-------------","-------------"));
            for(int i=0;i<inpData.getLossesArrayList().size();i++)
            {

                bufferedWriter.write(String.format(String.format("%-18s %-18s %-18s %-14s %-14s %-14s"+EOL,inpData.getLossesArrayList().get(i).getCulvert(),
                        inpData.getLossesArrayList().get(i).getEntry_loss_coeff(),inpData.getLossesArrayList().get(i).getExit_loss_coeff(),inpData.getLossesArrayList().get(i).getAvg_loss_coeff(),
                        inpData.getLossesArrayList().get(i).getFlap_gate(),inpData.getLossesArrayList().get(i).getSeepage_loss_rate())));

            }
            bufferedWriter.newLine();
            bufferedWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void writeCurve(BufferedWriter bufferedWriter)
    {
        try {
            bufferedWriter.write("[CURVES]"+EOL);
            bufferedWriter.write(String.format("%-18s %-18s %-14s %-14s"+EOL,";;Curve","Type","X-Value","Y-Value"));
            bufferedWriter.write(String.format("%-18s %-18s %-14s %-14s"+EOL,";;--------------","----------------","-------------","-------------"));
            for(int i=0;i<inpData.getCurveArrayList().size();i++)
            {
                int flap=0;
                if(inpData.getCurveArrayList().get(i).getDescription().size()!=0)
                {
                    for(int j=0;j<inpData.getCurveArrayList().get(i).getDescription().size();j++)
                    {
                        bufferedWriter.write(String.format("%s%s"+EOL,";",inpData.getCurveArrayList().get(i).getDescription().get(j)));
                    }
                }

                for(int j=0;j<inpData.getCurveArrayList().get(i).getXyArrayList().size();j++)
                {
                    if(flap==0) {
                        bufferedWriter.write(String.format(String.format("%-18s %-18s %-14s %-14s"+EOL, inpData.getCurveArrayList().get(i).getCurve(),
                                inpData.getCurveArrayList().get(i).getType(), inpData.getCurveArrayList().get(i).getXyArrayList().get(j).getX_value(),
                                inpData.getCurveArrayList().get(i).getXyArrayList().get(j).getY_value())));
                        flap++;
                    }
                    else
                    {
                        bufferedWriter.write(String.format(String.format("%-18s %-18s %-14s %-14s"+EOL, inpData.getCurveArrayList().get(i).getCurve(),
                                "", inpData.getCurveArrayList().get(i).getXyArrayList().get(j).getX_value(),
                                inpData.getCurveArrayList().get(i).getXyArrayList().get(j).getY_value())));
                    }
                }
                if(i<inpData.getCurveArrayList().size()-1)
                {
                    bufferedWriter.write(";"+EOL);
                }

            }
            bufferedWriter.newLine();
            bufferedWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void writeTimeseries(BufferedWriter bufferedWriter)
    {
        try {
            bufferedWriter.write("[TIMESERIES]"+EOL);
            bufferedWriter.write(String.format("%-18s %-18s %-14s %-14s"+EOL,";;Time Series","Date","Time","Value"));
            bufferedWriter.write(String.format("%-18s %-18s %-14s %-14s"+EOL,";;--------------","----------------","-------------","-------------"));
            for(int i=0;i<inpData.getTimeseriesArrayList().size();i++)
            {
                if(inpData.getTimeseriesArrayList().get(i).getDescription().size()!=0)
                {
                    for(int j=0;j<inpData.getTimeseriesArrayList().get(i).getDescription().size();j++)
                    {
                        bufferedWriter.write(String.format("%s%s"+EOL,";",inpData.getTimeseriesArrayList().get(i).getDescription().get(j)));
                    }
                }

                for(int j=0;j<inpData.getTimeseriesArrayList().get(i).getDateTimeValueArrayList().size();j++)
                {
                    bufferedWriter.write(String.format(String.format("%-18s %-18s %-14s %-14s"+EOL, inpData.getTimeseriesArrayList().get(i).getTimeseries(),
                            inpData.getTimeseriesArrayList().get(i).getDateTimeValueArrayList().get(j).getDate(), inpData.getTimeseriesArrayList().get(i).getDateTimeValueArrayList().get(j).getTime(),
                            inpData.getTimeseriesArrayList().get(i).getDateTimeValueArrayList().get(j).getValue())));
                }
                if(i<inpData.getTimeseriesArrayList().size()-1)
                {
                    bufferedWriter.write(";"+EOL);
                }
                bufferedWriter.flush();
            }
            bufferedWriter.newLine();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void writeReport(BufferedWriter bufferedWriter)
    {
        try {
            bufferedWriter.write("[REPORT]"+EOL);
            bufferedWriter.write(String.format("%-12s %s"+EOL,";;Reporting","Options"));
            bufferedWriter.write(String.format("%-12s %s"+EOL,"INPUT",inpData.getReport().getInput()));
            bufferedWriter.write(String.format("%-12s %s"+EOL,"CONTROLS",inpData.getReport().getControls()));

            bufferedWriter.write(String.format("%-12s","SUBCATCHMENTS"));
            for(int i=0;i<inpData.getReport().getSubcatchments().length;i++)
            {
                bufferedWriter.write(String.format(String.format(" %s",inpData.getReport().getSubcatchments()[i])));
            }
            bufferedWriter.write(EOL);

            bufferedWriter.write(String.format("%-12s","NODES"));
            for(int i=0;i<inpData.getReport().getNodes().length;i++)
            {
                bufferedWriter.write(String.format(String.format(" %s",inpData.getReport().getNodes()[i])));
            }
            bufferedWriter.write(EOL);

            bufferedWriter.write(String.format("%-12s","LINKS"));
            for(int i=0;i<inpData.getReport().getLinks().length;i++)
            {
                bufferedWriter.write(String.format(String.format(" %s",inpData.getReport().getLinks()[i])));
            }
            bufferedWriter.write(EOL);

            bufferedWriter.newLine();
            bufferedWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void writeTag(BufferedWriter bufferedWriter)
    {
        try {
            bufferedWriter.write("[TAGS]"+EOL);

            for(int i=0;i<inpData.getTagArrayList().size();i++)
            {
                bufferedWriter.write(String.format(String.format("%-18s %-18s %s"+EOL,inpData.getTagArrayList().get(i).getCategory(),inpData.getTagArrayList().get(i).getTagId(),inpData.getTagArrayList().get(i).getContent())));
            }

            bufferedWriter.newLine();
            bufferedWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void writeMap(BufferedWriter bufferedWriter)
    {
        try {
            bufferedWriter.write("[MAP]"+EOL);

            bufferedWriter.write("DIMENSIONS");
            for(int i=0;i<inpData.getMap().getDimensions().length;i++)
            {
                bufferedWriter.write(String.format(String.format(" %s",inpData.getMap().getDimensions()[i])));
            }
            bufferedWriter.write(EOL);

            bufferedWriter.write(String.format("%-10s %s"+EOL,"Units",inpData.getMap().getUnits()));

            bufferedWriter.newLine();
            bufferedWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void writeCoordinate(BufferedWriter bufferedWriter)
    {
        try {
            bufferedWriter.write("[COORDINATES]"+EOL);
            bufferedWriter.write(String.format("%-18s %-18s %-18s"+EOL,";;TreeNode","X-Coord","Y-Coord"));
            bufferedWriter.write(String.format("%-18s %-18s %-18s"+EOL,";;--------------","--------------","--------------"));
            for(int i=0;i<inpData.getCoordinateArrayList().size();i++)
            {
                bufferedWriter.write(String.format("%-18s %-18s %-18s"+EOL,inpData.getCoordinateArrayList().get(i).getNode(),inpData.getCoordinateArrayList().get(i).getX_coord(),inpData.getCoordinateArrayList().get(i).getY_coord()));
            }
            bufferedWriter.newLine();
            bufferedWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void writeVertice(BufferedWriter bufferedWriter)
    {
        try {
            bufferedWriter.write("[VERTICES]"+EOL);
            bufferedWriter.write(String.format("%-18s %-18s %-18s"+EOL,";;Link","X-Coord","Y-Coord"));
            bufferedWriter.write(String.format("%-18s %-18s %-18s"+EOL,";;--------------","--------------","--------------"));
            for(int i=0;i<inpData.getVerticeArrayList().size();i++)
            {
                bufferedWriter.write(String.format("%-18s %-18s %-18s"+EOL,inpData.getVerticeArrayList().get(i).getLink(),inpData.getVerticeArrayList().get(i).getX_coord(),inpData.getVerticeArrayList().get(i).getY_coord()));
            }
            bufferedWriter.newLine();
            bufferedWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void writePolygon(BufferedWriter bufferedWriter)
    {
        try {
            bufferedWriter.write("[Polygons]"+EOL);
            bufferedWriter.write(String.format("%-18s %-18s %-18s"+EOL,";;Subcatchment","X-Coord","Y-Coord"));
            bufferedWriter.write(String.format("%-18s %-18s %-18s"+EOL,";;--------------","--------------","--------------"));
            //使用foreach访问集合中的值，但不能访问到集合元素本身，只能访问到集合元素的值，因此不能改变集合元素
            for(InpData.Polygon polygon : inpData.getPolygonArrayList())
            {
                bufferedWriter.write(String.format("%-18s %-18s %-18s"+EOL,polygon.getPolygon(),polygon.getX_coord(),polygon.getY_coord()));
            }

//            for(int i=0;i<inpData.getPolygonArrayList().size();i++)
//            {
//                bufferedWriter.write(String.format("%-18s %-18s %-18s"+EOL,inpData.getPolygonArrayList().get(i).getPolygon(),inpData.getPolygonArrayList().get(i).getX_coord(),inpData.getPolygonArrayList().get(i).getY_coord()));
//            }
            bufferedWriter.newLine();
            bufferedWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void writeSymbol(BufferedWriter bufferedWriter)
    {
        try {
            bufferedWriter.write("[SYMBOLS]"+EOL);
            bufferedWriter.write(String.format("%-18s %-18s %-18s"+EOL,";;Gage","X-Coord","Y-Coord"));
            bufferedWriter.write(String.format("%-18s %-18s %-18s"+EOL,";;--------------","--------------","--------------"));
            for(int i=0;i<inpData.getSymbolArrayList().size();i++)
            {
                bufferedWriter.write(String.format("%-18s %-18s %-18s"+EOL,inpData.getSymbolArrayList().get(i).getGage(),inpData.getSymbolArrayList().get(i).getX_coord(),inpData.getSymbolArrayList().get(i).getY_coord()));
            }
            bufferedWriter.newLine();
            bufferedWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
